package com.quectel.core.module.prewarning.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.prewarning.dao.PreWarningConfigDao;
import com.quectel.core.module.prewarning.dto.PreWarningConfigDto;
import com.quectel.core.module.prewarning.entity.PreWarningConfigEntity;
import com.quectel.core.module.prewarning.service.PreWarningConfigService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-11-29 13:13:54
 */
@DubboService
public class PreWarningConfigServiceImpl implements PreWarningConfigService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PreWarningConfigServiceImpl.class);

    private static final String PRE_WARNING_CONFIG_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "PRE_WARNING_CONFIG_CACHE_BY_ID:";

    private static final String PRE_WARNING_CONFIG_VILLAGE =
            RedisCacheConstants.PROJECT_NAME + "PRE_WARNING_CONFIG_VILLAGE:";

    @Autowired
    private PreWarningConfigDao preWarningConfigDao;


    /**
     * 查询一条规则
     *
     * @param villageId
     * @param preWarnType
     * @return
     */
    @Override
    public PreWarningConfigDto selectOne(Long villageId, Byte preWarnType) {
        PreWarningConfigEntity entity = preWarningConfigDao.selectOne(
                new LambdaQueryWrapper<PreWarningConfigEntity>()
                        .eq(PreWarningConfigEntity::getVillageId, villageId)
                        .eq(PreWarningConfigEntity::getPreWarnType, preWarnType)
        );
        return paddingField(CopyUtils.copyObj(entity, PreWarningConfigDto.class));
    }

    @Override
    public PreWarningConfigDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                PRE_WARNING_CONFIG_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public PreWarningConfigDto selectById(Long id) {
        PreWarningConfigEntity entity = preWarningConfigDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, PreWarningConfigDto.class));
    }

    @Override
    public List<PreWarningConfigDto> queryList(Map<String, Object> params) {
        List<PreWarningConfigEntity> list = preWarningConfigDao.queryList(params);
        List<PreWarningConfigDto> result = CopyUtils.copyList(list, PreWarningConfigDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private PreWarningConfigDto paddingField(PreWarningConfigDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return preWarningConfigDao.queryTotal(params);
    }

    @Override
    public Long save(PreWarningConfigDto dto) {
        PreWarningConfigEntity entity = CopyUtils.copyObj(dto, PreWarningConfigEntity.class);
        entity.setId(Snowflake.nextId());
        preWarningConfigDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(PreWarningConfigDto dto) {
        PreWarningConfigEntity entity = CopyUtils.copyObj(dto, PreWarningConfigEntity.class);
        preWarningConfigDao.updateById(entity);
        CacheKit.invalidRedisCache(PRE_WARNING_CONFIG_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(PreWarningConfigDto dto) {
        PreWarningConfigEntity entity = CopyUtils.copyObj(dto, PreWarningConfigEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, preWarningConfigDao);

        CacheKit.invalidRedisCache(PRE_WARNING_CONFIG_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        preWarningConfigDao.deleteById(id);

        CacheKit.invalidRedisCache(PRE_WARNING_CONFIG_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
